$(function () {
    var cacheObj = {}
    var timer = null
    function fangdou(kw) {
        timer = setTimeout(function () {
            getSuggestList(kw)
        }, 500)
    }

    $('.ipt').on('keyup', function () {
        clearTimeout(timer)
        var keywords = $(this).val().trim()
        if (keywords.length <= 0) {
            return $('#Suggest-list').empty().hide()
        }
        if (cacheObj[keywords]) {
            console.log(cacheObj);
           return renderSuggest(cacheObj[keywords])
        }
        fangdou(keywords)

    })

    function getSuggestList(kw) {
        $.ajax({
            url: 'https://suggest.taobao.com/sug?q=' + kw,
            dataType: 'jsonp',
            success: function (res) {
                // console.log(res);
                renderSuggest(res)
            }

        })
    }

    // 渲染ui 
    function renderSuggest(res) {
        var htmlStr = template('tpl-suggestList', res)
        if (res.result.length <= 0) {
            return $('#Suggest-list').empty().hide()
        }
        $('#Suggest-list').html(htmlStr).show()
        var k = $('.ipt').val().trim()
        console.log(k);
        cacheObj[k] = res
    }
})